package com.example.truetimedemo.core

import com.instacart.truetime.TrueTimeEventListener
import com.instacart.truetime.time.TrueTimeParameters
import java.net.InetAddress
import java.util.Date

/**
 * @author huangzehui
 * @date 2024/6/6 18:57 周四
 * @description TrueTime 的一些执行回调
 */
class TrueTimeLogEventListener : TrueTimeEventListener {

    companion object {
        private const val LOG_TAG = "TrueTime执行回调"
    }


    override fun initialize(params: TrueTimeParameters) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("initialize call performed with $params")
        }
    }

    override fun initializeFailed(e: Exception) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("initialization call failed with a generic exception ${e.message}")
        }
    }

    override fun initializeSuccess(ntpResult: LongArray) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("came back successfully with $ntpResult")
        }
    }

    override fun lastSntpRequestAttempt(ipHost: InetAddress) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("This is the last SNTP request attempt to $ipHost")
        }
    }

    override fun nextInitializeIn(delayInMillis: Long) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append(" next initialization call will be made in ${delayInMillis} ms")
        }
    }

    override fun resolvedNtpHostToIPs(ntpHost: String, ipList: List<InetAddress>) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("resolved NTP pool host address $ntpHost to the list of IP addresses $ipList")
        }
    }

    override fun returningDeviceTime() {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("returning Device Time")
        }
    }

    override fun returningTrueTime(trueTime: Date) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("returning TrueTime $trueTime")
        }
    }

    override fun sntpRequest(address: InetAddress) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("SNTP request to $address")
        }
    }

    override fun sntpRequestFailed(e: Exception) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("SNTP request failed ${e}")
        }
    }

    override fun sntpRequestFailed(address: InetAddress, e: Exception) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("SNTP request failed ${address} ${e}")
        }
    }

    override fun sntpRequestSuccessful(address: InetAddress) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("SNTP Request to $address came back successfully")
        }
    }

    override fun storingTrueTime(ntpResult: LongArray) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("TimeKeeper storing time $ntpResult")
        }
    }

    override fun syncDispatcherException(t: Throwable) {
        TimeSyncManager.showLog(LOG_TAG) {
            it.append("CoroutineDispatcher exception from TrueTime sync call ${t}")
        }
    }
}